<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Db;
class Adminuser extends BaseController
{
    public function index()
    {
        
        
        return $this->fetch();
    }

    public function manage()
    {
        $m=db('admin');
        //$rs=$m->limit(10)->select();
		$zong=$m->count('user_id');
		$rs=$m->paginate(10,$zong);
        $this->assign('zong',$zong);
        $this->assign('admin',$rs);
        return $this->fetch();
    }
	


    public function insert_form()
    {
        $m=db('admin_group');
        $rs=$m->select();
        $this->assign('admin',$rs);
        return $this->fetch();
    }
    

	

	
    public function update_form()
    {

            $id=input('param.id',0,'intval');

        $m=db('admin');
        
        $rs=$m->find($id);
        $this->assign('admin',$rs);
        
        $a=db('admin_group');
        $a=$a->select();

        $this->assign('admin1',$a);

        return $this->fetch();
    }

    //删除用户
    public function ajax_delete()
    {


        $id=input('param.id');

        $m=db('admin');
        $ok=$m->delete($id);
        if($ok>0){
            $arr['icon']=1;
            $arr['msg']='删除成功';

        }else{
            $arr['icon']=5;
            $arr['msg']='删除失败';
        }
        return json( $arr);
    }
    //用户更新
    public function ajax_update()
    {


        $id=input('param.user_id');
        if(input('param.login_password1')==input('param.login_password2') && input('param.login_password1')!=''){
            // $m=  db('admin');
//                $ok=$m->where('login_password',substr(md5(input('post.login_password')),15,25))->find();
//
//                if($ok){
            $data=[
                'login_password'=>substr(md5(input('param.login_password1')),15,25),
                'status'=>input('post.status'),
                'group_id'=>input('post.group_id',0)
            ];
            //  }else{
//                    $this->error('原密码输入错误！');
//                }
        }else if(input('param.login_password1')=='' && input('param.login_password2')==''){
            $data=[
                'status'=>input('param.status'),
                'group_id'=>input('param.group_id',0)
            ];
        }else{
            $arr['icon']=5;
            $arr['msg']='两次密码不一致';

        }


        $m=db('admin');
        @$ok=$m->where('user_id',$id)->update($data);
        if($ok>0){
            $arr['icon']=1;
            $arr['msg']='修改成功';
        }else{
            $arr['icon']=5;
            $arr['msg']='修改失败';

        }
        return json($arr);

    }


    //用户添加
    public function ajax_insert()
    {
        $arr=array();

        if( input('param.login_account')!='' && input('param.login_password')!='' && input('param.login_password2')!=''){
            if(input('param.login_password')==input('param.login_password2') && input('param.login_password')!=''){

                $m=  db('admin');
                $ok=$m->where('login_account',input('param.login_account'))->find();

                if($ok){
                    $arr['icon']=5;
                    $arr['msg']='已有该用户名！';
                }else{
                    $data=[
                        'login_account'=>input('param.login_account'),
                        'login_password'=>substr(md5(input('param.login_password')),15,25),
                        'status'=>input('post.status'),
                        'group_id'=>input('post.group_id',0)
                    ];

                }

            }else{

                $arr['icon']=5;
                $arr['msg']='两次密码不一致';

            }

        }
        $m=db('admin');
        @$ok=$m->insert($data);

        if($ok>0){

            $arr['icon']=1;
            $arr['msg']='添加成功';

        }else{

            $arr['icon']=5;
            $arr['msg']='添加失败';

        }
        return json($arr);
    }
	
}
